using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PokerLearner.Poker
{
	public class ExporterC50 : IExporter
	{
		#region Export

		public void Export(string name)
		{
			ExportNames(name);
			ExportData(name);			
		}

		public void ExportNames(string name)
		{
			FileStream namesStream = new FileStream(name + ".names", FileMode.Create);
			StreamWriter writer = new StreamWriter(namesStream);

			writer.WriteLine("win.");
			writer.WriteLine();
			writer.WriteLine("id: label.");
			writer.WriteLine("position: Normal, SmallBlind, BigBlind, Dealer");
			writer.WriteLine("numplayers: continuos.");
			writer.WriteLine("preflophand: continuous.");
			writer.WriteLine("preflopnumplays: continuos.");
			writer.WriteLine("preflopplay: None, Check, Fold, Call, Bet, Raise, AllIn.");
			writer.WriteLine("preflopplayvalue: continuous.");
			writer.WriteLine("numplayersflop: continuos.");
			writer.WriteLine("flophand: continuous.");
			writer.WriteLine("flopcommonhand: continuous.");
			writer.WriteLine("flopnumplays: continuos.");
			writer.WriteLine("flopplay: None, Check, Fold, Call, Bet, Raise, AllIn.");
			writer.WriteLine("flopplayvalue: continuous.");
			writer.WriteLine("numplayersturn: continuous.");
			writer.WriteLine("turnhand: continuous.");
			writer.WriteLine("turncommonhand: continuous.");
			writer.WriteLine("turnnumplays: continuos.");
			writer.WriteLine("turnplay: None, Check, Fold, Call, Bet, Raise, AllIn.");
			writer.WriteLine("turnplayvalue: continuous.");
			writer.WriteLine("numplayersriver: continuous.");
			writer.WriteLine("riverhand: continuous.");
			writer.WriteLine("rivercommonhand: continuous.");
			writer.WriteLine("rivernumplays: continuos.");
			writer.WriteLine("riverplay: None, Check, Fold, Call, Bet, Raise, AllIn.");
			writer.WriteLine("riverplayvalue: continuous.");
			writer.WriteLine("win: True, False.");

			writer.Close();
		}

		public void ExportData(string name)
		{
			FileStream dataStream = new FileStream(name + ".data", FileMode.Create);
			StreamWriter writer = new StreamWriter(dataStream);
			foreach (Record record in PokerLogger.Instance.PermanentRecords)
			{
				// Write identifier, position and preflop hand
				writer.Write("{0},{1},{2},{3},", record.Identifier, record.Position, record.NumPlayers, record.PreFlopHand.Value);

				if (record.PreFlopPlays != null)
					writer.Write("{0},{1},{2},", record.PreFlopPlays.Count, 
						record.PreFlopPlays[record.PreFlopPlays.Count - 1].Type, 
						Record.FindTotalPlayValue(record.PreFlopPlays));
				else
					writer.Write("0,None,0,");

				writer.Write("{0},", record.NumPlayersFlop);

				if (record.FlopHand != null)
					writer.Write("{0},{1},", record.FlopHand.Value, record.FlopCommonHand.Value);
				else
					writer.Write("0,0,");

				if (record.FlopPlays != null)
					writer.Write("{0},{1},{2},", record.FlopPlays.Count,
						record.FlopPlays[record.FlopPlays.Count - 1].Type,
						Record.FindTotalPlayValue(record.FlopPlays));
				else
					writer.Write("0,None,0,");

				writer.Write("{0},", record.NumPlayersTurn);

				if (record.TurnHand != null)
					writer.Write("{0},{1},", record.TurnHand.Value, record.TurnCommonHand.Value);
				else
					writer.Write("0,0,");

				if (record.TurnPlays != null)
					writer.Write("{0},{1},{2},", record.TurnPlays.Count,
						record.TurnPlays[record.TurnPlays.Count - 1].Type,
						Record.FindTotalPlayValue(record.TurnPlays));
				else
					writer.Write("0,None,0,");

				writer.Write("{0},", record.NumPlayersRiver);

				if (record.RiverHand != null)
					writer.Write("{0},{1},", record.RiverHand.Value, record.RiverCommonHand.Value);
				else
					writer.Write("0,0,");

				if (record.RiverPlays != null)
					writer.Write("{0},{1},{2},", record.RiverPlays.Count,
						record.RiverPlays[record.RiverPlays.Count - 1].Type,
						Record.FindTotalPlayValue(record.RiverPlays));
				else
					writer.Write("0,None,0,");

				writer.WriteLine("{0}", record.Win);
			}
			writer.Close();
		}

		#endregion
	}
}
